/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.saber.certificacao.beansjsf.login;
/*
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.RememberMeServices;
import org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationException;
import org.jasypt.util.text.BasicTextEncryptor;
import br.com.saber.certificacao.ejbs.stateless.interfaces.UsuarioBeanLocal;
import br.com.saber.certificacao.modelos.Usuario;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.springframework.security.authentication.RememberMeAuthenticationToken;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.dao.DataAccessException;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
 * URL
 *http://git.springsource.org/spring-security/spring-security/blobs/56e86dd36f575595dfb40def92776e08f3ddec8f/web/src/main/java/org/springframework/security/web/authentication/rememberme/AbstractRememberMeServices.java
 * @author Luke Taylor
 * @copy by Guilherme
 * 
 */
public class MyRememberMeServices /*implements  RememberMeServices, LogoutHandler,  UserDetailsService, PasswordEncoder*/ {
  /*  private UsuarioBeanLocal usuarioBean;
    private String cookieName = "SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY";

    @Override
    public Authentication autoLogin(HttpServletRequest request, HttpServletResponse response) {
        String rememberMeCookie = extractRememberMeCookie(request);
        if (rememberMeCookie == null) {
            return null;
        }
        if (rememberMeCookie.length() == 0) {
            cancelCookie(request, response);
            return null;
        }

        UserDetails user = null;

        try {
            String cookieTokens = decodeCookie(rememberMeCookie);
            user = processAutoLoginCookie(cookieTokens);
            return createSuccessfulAuthentication(user);
        } catch (UsernameNotFoundException unfe) {
            cancelCookie(request, response);
            return null;
        }
    }

    protected void cancelCookie(HttpServletRequest request, HttpServletResponse response) {
        Cookie cookie = new Cookie(cookieName, null);
        cookie.setMaxAge(0);
        cookie.setPath(getCookiePath(request));
        response.addCookie(cookie);
    }

    private String getCookiePath(HttpServletRequest request) {
        String contextPath = request.getContextPath();
        return contextPath.length() > 0 ? contextPath : "/";
    }

    protected String extractRememberMeCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if ((cookies == null) || (cookies.length == 0)) {
            return null;
        }
        for (Cookie cookie : cookies) {
            if (cookieName.equals(cookie.getName())) {
                return cookie.getValue();
            }
        }
        return null;
    }

    private String encodeCookie(String cookie) {
        BasicTextEncryptor bte = new BasicTextEncryptor();
        bte.setPassword("Metallica");
        return bte.encrypt(cookie);
    }

    private String decodeCookie(String cookie) {
        BasicTextEncryptor bte = new BasicTextEncryptor();
        bte.setPassword("Metallica");
        return bte.decrypt(cookie);
    }

    @Override
    public void loginFail(HttpServletRequest request, HttpServletResponse response) {
       cancelCookie(request, response);
    }

    @Override
    public void loginSuccess(HttpServletRequest request, HttpServletResponse response, Authentication successfulAuthentication) {
        setCookie(successfulAuthentication.getName(), request, response);
    }

    private void setCookie(String cookieValue, HttpServletRequest request, HttpServletResponse response) {
        cookieValue = encodeCookie(cookieValue);
        Cookie cookie = new Cookie(cookieName, cookieValue);
        cookie.setMaxAge(90 * 24 * 60 * 60);
        cookie.setPath(getCookiePath(request));
        response.addCookie(cookie);
    }

    private UserDetails processAutoLoginCookie(String cookieTokens) throws UsernameNotFoundException {
        UsuarioBeanLocal usuarioBean = lookupUsuarioBeanLocal();
        Usuario toReturn = null;
        toReturn = usuarioBean.buscarUsuarioLogin(cookieTokens);
        if (toReturn != null && toReturn.isAccountNonLocked()) {
            return toReturn;
        }
        throw new UsernameNotFoundException("Usuario não encontrado.");
    }

    protected Authentication createSuccessfulAuthentication(UserDetails user) {
        RememberMeAuthenticationToken auth = new RememberMeAuthenticationToken(user.getUsername(), user, user.getAuthorities());
        return auth;
    }

    private UsuarioBeanLocal lookupUsuarioBeanLocal() {
        try {
            Context c = new InitialContext();
            return (UsuarioBeanLocal) c.lookup("java:global/Saber/saber-ejb/UsuarioBean!br.com.saber.certificacao.ejbs.stateless.interfaces.UsuarioBeanLocal");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    @Override
    public void logout(HttpServletRequest hsr, HttpServletResponse hsr1, Authentication a) {
        
    }
    
      @Override
    public String encodePassword(String string, Object o) throws DataAccessException {
        if (this.usuarioBean == null) {
            this.usuarioBean = this.lookupUsuarioBeanLocal();
        }
        Usuario userPass = this.usuarioBean.buscarUsuarioLogin(o.toString());
        return this.usuarioBean.criptografarSenha(string, userPass.getRoles());
    }

    @Override
    public boolean isPasswordValid(String string, String string1, Object o) throws DataAccessException {
        if (this.usuarioBean == null) {
            this.usuarioBean = this.lookupUsuarioBeanLocal();
        }
        Usuario us = this.usuarioBean.buscarUsuarioLogin(o.toString());
        return us.getPassword().equals(this.usuarioBean.criptografarSenha(string1, us.getRoles()));
    }

    @Override
    public UserDetails loadUserByUsername(String string) throws UsernameNotFoundException, DataAccessException {
        if (this.usuarioBean == null) {
            this.usuarioBean = this.lookupUsuarioBeanLocal();
        }
        Usuario usuario = this.usuarioBean.buscarUsuarioLogin(string);
        if (usuario == null) {
            throw new UsernameNotFoundException("Usuário não encontrado: " + string);
        }
        return usuario;
    }
    */
}
